﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using DataEntities;
using DataProvider;
using SQLDataProvider;
using BusinessServices.Enumerates;
using System.Linq;

namespace BusinessServices
{
    public class KeHuService
    {
        private readonly IBaseRepository<KeHuInfo> _keHuRepository;
        private readonly ChuZuService _chuZuService;
        private readonly TuiHuanService _tuiHuanService;

        public KeHuService()
        {
            _keHuRepository = new BaseRepository<KeHuInfo>();
            _chuZuService = new ChuZuService();
            _tuiHuanService = new TuiHuanService();
        }

        /// <summary>
        /// 根据客户状态获取客户信息
        /// </summary>
        /// <param name="status">客户状态</param>
        public List<KeHuInfo> GetAll(KeHuStatus status)
        {
            switch (status)
            {
                case KeHuStatus.IsActive:
                    return _keHuRepository.GetAll(c => c.bLogout);
                case KeHuStatus.IsLogout:
                    return _keHuRepository.GetAll(c => !c.bLogout);
                default:
                    return _keHuRepository.GetAll(c => true);
            }
        }

        /// <summary>
        /// 获取没有租车的客户信息
        /// </summary>
        public List<KeHuInfo> GetNewKeHuInfo()
        {
            return _keHuRepository.GetAll(c => !c.bLogout && c.ChuZuInfo.Count(o => !o.bCZZF) == 0 && c.YuZuInfo.Count == 0);
        }

        /// <summary>
        /// 获取预租了车辆的客户信息
        /// </summary>
        public List<KeHuInfo> GetYuZuKeHuInfo()
        {
            return _keHuRepository.GetAll(c => !c.bLogout && c.YuZuInfo.Count > 0, JoinCarAndYuZu);
        }

        /// <summary>
        /// 获取所有有租车记录的客户信息
        /// </summary>
        /// <returns></returns>
        public List<KeHuInfo> GetKeHuInfoByChuZuRecord()
        {
            return _keHuRepository.GetAll(c => c.ChuZuInfo.Count > 0);
        }

        /// <summary>
        /// 获取出租中的未违章的用户信息
        /// </summary>
        public List<KeHuInfo> GetNeverWeiZhangKeHuInfo()
        {
            return _keHuRepository.GetAll(c => c.ChuZuInfo.Count(o => !o.bCZZF) > 0 && c.WeiZhangInfo.Count(w => !w.bWZZF) == 0);
        }

        /// <summary>
        /// 获取出租中的违章的用户信息
        /// </summary>
        public List<KeHuInfo> GetWeiZhangKeHuInfo()
        {
            return _keHuRepository.GetAll(c => c.ChuZuInfo.Count(o => !o.bCZZF) > 0 && c.WeiZhangInfo.Count(w => !w.bWZZF) > 0);
        }

        /// <summary>
        /// 获取所有违章的用户信息
        /// </summary>
        /// <returns></returns>
        public List<KeHuInfo> GetAllWeiZhangeKeHuInfo()
        {
            return _keHuRepository.GetAll(c => c.ChuZuInfo.Count > 0 && c.WeiZhangInfo.Count > 0);
        }

        /// <summary>
        /// 获取出租单已到期的客户信息
        /// </summary>
        public List<KeHuInfo> GetKeHuInfoWhereChuZuTimeOut()
        {
            var timeOutChuZuInfo = _chuZuService.GetTimeOutChuZuInfo();
            return _keHuRepository.GetAll(c => timeOutChuZuInfo.Select(o => o.iKeHuId).Contains(c.iKeHuId));
        }

        /// <summary>
        /// 获取出租单未到期的客户信息
        /// </summary>
        public List<KeHuInfo> GetKeHuInfoWhereChuZuNotTimeOut()
        {
            var notTimeOutChuZuInfo = _chuZuService.GetNotTimeOutChuZuInfo();
            return _keHuRepository.GetAll(c => notTimeOutChuZuInfo.Select(o => o.iKeHuId).Contains(c.iKeHuId));
        }

        /// <summary>
        /// 获取已结算的客户信息
        /// </summary>
        /// <returns></returns>
        public List<KeHuInfo> GetKeHuInfoWhereChuZuClosed()
        {
            var tuiHuanInfo = _tuiHuanService.GetAll();
            return _keHuRepository.GetAll(c => tuiHuanInfo.Select(t => t.iKeHuId).Contains(c.iKeHuId));
        }

        /// <summary>
        /// 添加一个新客户
        /// </summary>
        public KeHuInfo Add(KeHuInfo keHuInfo)
        {
            return _keHuRepository.Add(keHuInfo);
        }

        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="keHuId">客户Id</param>
        /// <param name="update">包含更新的委托，在此委托中进行数据更改操作</param>
        public KeHuInfo Update(int keHuId, Action<KeHuInfo> update)
        {
            return _keHuRepository.Update(p => p.iKeHuId == keHuId, update);
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="keHuId">客户Id</param>
        public bool Delete(int keHuId)
        {
            return _keHuRepository.Delete(c => c.iKeHuId == keHuId);
        }

        /// <summary>
        /// 根据客户身份证号判断客户是否存在 
        /// </summary>
        /// <param name="idCard">身份证号</param>
        public bool IsExisted(string idCard)
        {
            return _keHuRepository.Count(c => string.Equals(c.sIdCard, idCard, StringComparison.OrdinalIgnoreCase)) > 0;
        }

        //Join 预租信息表和车辆信息表
        private DataLoadOptions JoinCarAndYuZu()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<KeHuInfo>(c => c.YuZuInfo);
            loadOption.LoadWith<YuZuInfo>(y => y.CarInfo);
            return loadOption;
        }

    }
}
